<?php
load_info_var($cf_controller);	   //加载smarty变量
load_file('classroom_content_class', LIB_PATH);
$main = new ClassroomContent($db);
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$self = isset($_POST['self']) ? trim($_POST['self']) : trim($_GET['self']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;

switch ($cf_action) {
    case "index":    
	$smarty->mypage($cf_controller, $cf_action, 0);
	$stype = isset($_POST['stype']) ? trim($_POST['stype']) : urldecode($_GET['stype']);	
	$skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode($_GET['skey']);
	$start = isset($_POST['start']) ? trim($_POST['start']) : urldecode($_GET['start']);
	$end = isset($_POST['end']) ? trim($_POST['end']) : urldecode($_GET['end']);	
	$where = '1';	
	if ($skey) {
	    $where .= " and crc_title like '%" . $skey . "%'";
	}	
	if ($start && $end) {
            $where .= " AND DATE_FORMAT(crc_create_time,'%Y-%m-%d')  BETWEEN '".$start."' AND '".$end."'";
    }	
	if ($stype) {
		$where .= " and crc_type = '" . $stype . "'";
	}

	load_file('page_class', LIB_PATH);
	$MyPage = new Mypage($db);
	$field = '*';
	$data_ary = $MyPage->from_sql($field, $main->get_table_name(), $where, "crc_id DESC", intval($_GET['cpage']), "20");
	$page = $MyPage->get_page($data, $cf_self_page . '?dpt=' . $cf_dpt .'&se;f=' . $self. '&skey=' . $skey . '&cate=' . $cate . '&brand=' . $brand . '&start=' . $start.'&end='.$end);
	
	//dump($data_ary);
	//echo $MyPage->get_sql();
	//dump($data_ary);
	break;
    case "add":
	if ($cf_ispost) {		
		$data = $db->getFormData($main->get_table_name(),$_POST);	
		$data['crc_create_time'] = date("Y-m-d H:i:s");	
	    $result = $main->insert_data($data);
	    if($result) {
		$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
		unset($_SESSION['pre_url']);
		$more = $_SERVER['HTTP_REFERER'] ? '<a onclick="my_close()" href="' . $_SERVER['HTTP_REFERER'] . '">[继续添加]</a>' : '';
		my_jump('添加成功' . $more, '', $return_page, 3);
	    } else {
		my_jump('添加失败');
	    }
	} else {
	    $smarty->mypage($cf_controller, $cf_action, 0);
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';			
	}

	break;
    case "edit":
	if ($cf_ispost) {	
		$data = $db->getFormData($main->get_table_name(),$_POST);
		$data['crc_edit_time'] = date("Y-m-d H:i:s");
	    $result = $main->update_data($data, "crc_id='$id'");
	    if ($result) {
		$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
		unset($_SESSION['pre_url']);
		    my_success('修改成功', '', $return_page, 3);
	    } else {
		    my_error('修改失败');
	    }
	} else {		 
	    $smarty->mypage($cf_controller, $cf_action, 0);
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
		$row = $main->get_data_row($id);
	    if (!$row)
		my_jump('沒有此信息');	   
	}
	break;
    case "del":
	$result = $main->del_data($id);
	if ($result) {
	    my_jump('删除成功', '', $return_page, 1);
	} else {
	    my_jump('删除失败!');
	}

	break;
	case "detail":
	$smarty->mypage($cf_controller, $cf_action, 0);
	$row = $main->get_data_row($id);
	if (!$row)
	my_jump('沒有此信息');
	break;  
   
	default:
	my_jump('非法操作!');
	break;
}


$smarty->assign(array(
    'id' => $id,
    'row' => $row,
    'data_ary' => $data_ary,
    'page' => $page,
    'skey' => $skey,
    'stype' => $stype,
	'start' => $start,
	'end' => $end,
	'type_ary' => add_array_item(array('' => '全部'),$cf_info_var_common['classroom_content_type']),
	
));

$smarty->mydisplay();
?>